#include "mainwindow.h"
#include "qfiledialog.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("CANTools");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_openButton_clicked(bool checked)
{
    if(checked == 0)
    {
        if(openState == false)
        {
            sCanBaud mBaud;
            mBaud.bUserDefBaud = 0;
            mBaud.nBaud = 500;
            sMarkers = ui->comboName->currentText();
            sDevType = ui->comboType->currentText();
            if(canIf->OpenDevice(sMarkers, sDevType, mBaud))
            {
                connect(&devDialog, &openDevDialog::updateTitleText, this, &MainWindow::setUserTitle);
                connect(&flashExecutor, &FlashExecutor::updateVersion, this, &MainWindow::setVersion);
                connect(&flashExecutor, &FlashExecutor::updateListMsg, this, &MainWindow::setlistMessage);
                devDialog.show();
                devDialog.exec();
                canIf->start();
                // connect(canIf, &CanIF::updateLabelText, this, &MainWindow::setLabelText);
                ui->openButton->setText("关闭设备");
                ui->listMessage->clear();
                openState = true;
                udsTp.bRunFlag = true;
            }
            else
            {
                ui->lineEdit->setText("设备打开失败!!!");
            }
        }
        else
        {
            disconnect(&devDialog, &openDevDialog::updateTitleText, this, &MainWindow::setUserTitle);
            disconnect(&udsTp, &UdsTp::updateTxMsg, this, &MainWindow::setTxMsgFlag);
            disconnect(canIf, &CanIF::receive, &flashExecutor, &FlashExecutor::handleRxMsg);
            disconnect(&flashExecutor, &FlashExecutor::updateVersion, this, &MainWindow::setVersion);
            disconnect(&flashExecutor, &FlashExecutor::updateListMsg, this, &MainWindow::setlistMessage);
            flashExecutor.u8RunningFlag = false;
            udsTp.bRunFlag = false;
            canIf->CloseDevice();
            ui->openButton->setText("打开设备");
            openState = false;
        }
    }
}


void MainWindow::on_sendButton_clicked(bool checked)
{
    if(checked == 0)
    {
        if(m_pConfig.u8ReadSuccFlag)
        {
            flashExecutor.Initialization();
            udsTp.Initialization();
            udsTp.start();
        }
    }
}


void MainWindow::setLabelText(sCANObj msg)
{
    QString message;
    message = QString("%1").arg(msg.ID,3,16,QLatin1Char(' '));
    for(uint8_t idx = 0; idx < 8; idx++)
    {
        // message = message + " " + QString::number(msg.Data[idx],16);
        message = message + " " + QString("%1").arg(msg.Data[idx],2,16,QLatin1Char('0'));
    }
    ui->lineEdit->setText(message);
}

void MainWindow::setTxMsgFlag(sCANObj msg)
{
    canIf->Send(msg);
}

void MainWindow::setUserTitle(QString title)
{
    this->setWindowTitle("CANTools -" + title);
    loadFile(title);
}

void MainWindow::setVersion(uint8_t *str, uint8_t strLen)
{
    QString version = "";

    for(uint8_t idx = 0; idx < strLen; idx++)
    {
        version += (char)str[idx];
    }

    ui->softVersion->setText(version);
}

void MainWindow::setlistMessage(QString msg, uint8_t type)
{
    QListWidgetItem *listWidIt = new QListWidgetItem(msg);
    if(type == 0)
    {
        // listWidIt->setForeground(QColor(50, 205, 50));
        listWidIt->setForeground(QColor(34,139,34));
    }
    else if(type == 1)
    {
        listWidIt->setForeground(QColor(0, 0, 0));
    }
    else
    {
        listWidIt->setForeground(QColor(255, 0, 0));
    }
    ui->listMessage->addItem(listWidIt);
    ui->listMessage->scrollToBottom();
}

void MainWindow::loadFile(QString &filepath)
{
    if(m_pConfig.readCfg(filepath, &flashExecutor.flashInfor, &udsTp.jsonTpInfor) == true)
    {
        connect(&udsTp, &UdsTp::updateTxMsg, this, &MainWindow::setTxMsgFlag);
        connect(canIf, &CanIF::receive, &flashExecutor, &FlashExecutor::handleRxMsg);
        flashExecutor.u8RunningFlag = m_pConfig.u8ReadSuccFlag;
        ui->lineEdit->setText("配置文件解析成功!");
    }
    else
    {
        ui->lineEdit->setText("配置文件错误，解析失败!!!");
    }
}

void MainWindow::on_comboName_currentIndexChanged(int index)
{
    QStringList DevList;
    if(index == 0)
    {
        ZlgCan mZlgDev;
        mZlgDev.FindDev(DevList);
    }
    else if(index == 1)
    {
        VectorCan mCanDev;
        mCanDev.FindDev(DevList);
    }
    else
    {
    }
    ui->comboType->clear();
    ui->comboType->addItems(DevList);
}


void MainWindow::on_selectApp_clicked(bool checked)
{
    if(checked == 0)
    {
        QString filePath = QFileDialog::getOpenFileName(this, "选择App文件", "", "File(*.hex)");
        if (!filePath.isEmpty()) {
            ui->AppPath->setText(filePath);
            flashExecutor.setAppPath(filePath);
        }
    }
}

